import os

from base64 import b64encode, b64decode
from datetime import datetime

from google.appengine.api import users
from google.appengine.ext.webapp import template

import model
import pyDes
import logging

def encrypt(clear_pass, secret):
    '''
    returns the base 64 encoded version of the
    encrypted password

    @param clear_pass: the clear_text password, make sure there is no
        trailing blank in it by stipping it before.
    @type clear_pass: string

    @param secret:
    @type secret:
    '''
    # format the secret for encryption
    secret = '%%-%ss' % 16 % secret

    # create a pyDes object
    my_key = pyDes.triple_des(secret)

    # encrypt the password
    bin_encrypted_pwd = my_key.encrypt(clear_pass, pad=' ')

    # encode the result in base64 for db
    b64_encrypted_pwd = b64encode(bin_encrypted_pwd)

    # return the result
    return b64_encrypted_pwd

def decrypt(b64hash, secret):
    '''
    returns the decrypted password

    @param b64hash: the base64 encoded hash encrypted in 3-des
    @type b64hash: string
    '''
    # format the secret for encryption
    secret = '%%-%ss' % 16 % secret

    # create a pyDes object
    my_key = pyDes.triple_des(secret)

    # decrypt the base64 string from the db
    bin_encrypted_pwd = b64decode(b64hash)

    # decrypt the binary string
    decrypted_pwd = my_key.decrypt(bin_encrypted_pwd, pad=' ')

    # return the clear password
    return decrypted_pwd

def new_visit():
    '''
    '''
    # visit key object
    visit = model.Visit()

    # init the number of attempts
    visit.number_of_attempts = 1

    # get the current user
    user = users.get_current_user()

    # set the current attempting user
    visit.user = user

    # save
    visit.put()

    # return the key of the visit key
    return visit

def get_visit():
    '''
    '''
    # visit key object
    visit = model.Visit()

    # get the current user
    user = users.get_current_user()

    # get the visits for the current user
    query = visit.all().filter('user =', user)

    try:
        # return the first visit
        if query[0]:
            return query[0]

    except:
        pass

    # no visit found we return a new key
    return new_visit()

def clear_visit():
    '''
    '''
    # visit key object
    visit = model.Visit()

    # get the current user
    user = users.get_current_user()

    # get the visits for the current user
    query = visit.all().filter('user =', user)

    for v in query:
        # clear the visits
        v.delete()

def display_message(response, title, message):
    '''
    '''
    # set the template
    template_values = { 'title': title,
                        'message': message }

    # root path
    root = os.path.dirname(__file__)

    # find the template
    path = os.path.join(root, 'templates', 'message.html')

    # display the page
    response.out.write(template.render(path, template_values))
